<template>
  <div class="department-container">
    <div class="app-container">
      <el-card>
        <!-- 用一个行列布局 -->
        <el-row type="flex" justify="space-between" align="middle" style="height: 40px">
          <el-col :span="20">
            <svg-icon icon-class="bug" /><span>江苏传智播客教育科技股份有限公司</span>
          </el-col>
          <el-col :span="4">
            <el-row type="flex" justify="end">
              <!-- 两个内容 -->
              <el-col>负责人</el-col>
              <!-- 下拉菜单 element -->
              <el-col>
                <el-dropdown>
                  <span>
                    操作<i class="el-icon-arrow-down" />
                  </span>
                  <el-dropdown-menu slot="dropdown">
                    <el-dropdown-item @click.native="hAdd(data.id)">添加子部门</el-dropdown-item>
                  </el-dropdown-menu>
                </el-dropdown>
              </el-col>
            </el-row>
          </el-col>
        </el-row>
        <!-- 架构 -->
        <el-tree
          :data="data"
          default-expand-all

          icon-class=" "
        >
          <template slot-scope="{data ,node}">
            <el-row type="flex" justify="space-between" align="middle" style="height: 40px ; width:100%">
              <el-col :span="20">
                <svg-icon v-if="data.children.length === 0" icon-class="staff" />
                <svg-icon v-else :icon-class="node.expanded ? 'jianhao' : 'jiahao'" />
                <span>{{ data.name }}</span>
              </el-col>
              <el-col :span="4">
                <el-row type="flex" justify="end">
                  <!-- 两个内容 -->
                  <el-col>{{ data.manager }}</el-col>
                  <!-- 下拉菜单 element -->
                  <el-col>
                    <el-dropdown>
                      <span>
                        操作<i class="el-icon-arrow-down" />
                      </span>
                      <el-dropdown-menu slot="dropdown">
                        <el-dropdown-item @click.native="hAdd(data.id)">添加子部门</el-dropdown-item>
                        <el-dropdown-item @click.native="hEdit(data.id)">编辑部门</el-dropdown-item>
                        <el-dropdown-item v-if="data.children.length===0" @click.native="hdel(data.id)">删除部门</el-dropdown-item>
                      </el-dropdown-menu>
                    </el-dropdown>
                  </el-col>
                </el-row>
              </el-col>
            </el-row>
          </template>
        </el-tree>
      </el-card>
    </div>
    <el-dialog
      :title=" isEdit ? '编辑' : '添加'"
      :visible.sync="dialogVisible"
      :close-on-click-modal="false"
      :close-on-press-escape="false"
    >
      <AddorEdit
        v-if="dialogVisible"
        :id="curId"
        :is-edit="isEdit"
        :origin-list="originList"
        @success="hSuccess"
        @close="hClose"
      />
    </el-dialog>

  </div>
</template>

<script>
import AddorEdit from './deptDialog.vue'
import { tranListToTreeData } from '@/utils/index'
import { getDepartment, delDepartments } from '@/api/department'
export default {
  components: {
    AddorEdit
  },
  data() {
    return {
      curId: '',
      dialogVisible: false,
      data: [],
      isEdit: false,
      originList: []
    }
  },
  created() {
    this.loadDepartment()
  },
  methods: {
    async loadDepartment() {
      const res = await getDepartment()
      console.log(res, 89080)
      res.data.depts.shift()
      this.data = tranListToTreeData(res.data.depts)
      this.originList = res.data.depts.map(item => {
        return { code: item.code, name: item.name, pid: item.pid, id: item.id }
      })
      console.log('map', this.originList)
    },
    // 点击添加子部门显示组件
    hAdd(id) {
      this.curId = id
      this.dialogVisible = true
      this.isEdit = false
    },
    hEdit(id) {
      this.curId = id
      this.dialogVisible = true
      this.isEdit = true
    },

    // 删除部门
    async doDel(id) {
      try {
        const res = await delDepartments(id)
        console.log(res, 99999)
        // 1. 提示
        this.$message({ type: 'success', message: '删除成功!' })
        // 2. 更新数据
        this.loadDepartment()
      } catch (err) {
        console.log(err)
        this.$message({ type: 'error', message: '删除失败' })
      }
    },
    hdel(id) {
      this.$confirm('此操作将永久删除部门, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        this.doDel(id)
      }).catch(() => {

      })
    },
    // 收到子组件中的success事件
    hSuccess() {
      this.dialogVisible = false
      this.loadDepartment()
    },
    hClose() {
      this.dialogVisible = false
    }
  }

}
</script>
<style>

</style>

